'use strict';

const util = require("util");
const Service = require('egg').Service;
const mysql = require('mysql');

const Koa = require('koa');
const app = new Koa();
const bodyParser = require('koa-bodyparser');

// 使用ctx.body解析中间件
app.use(bodyParser());

class equipmentService extends Service {
    //查询所有仪器信息的方法
    async  findEquipmentAll() {
        const result = await this.app.mysql.select('e_equipments')
        return {
            //查询成功，并返回数据
            code: 200,
            data: result,
            message: '查询成功！',
        };
    }

    //根据设备编号修改设备信息
    async updateEquipmentByNumber(equipment_number, equipment_name, equipment_price) {
        //查询的条件：设备编号equipment_number
        const options = {
            where: {
                equipment_number: equipment_number,
            }
        };
        //要修改的数据：设备名称和设备价格
        const row = {
            equipment_name: equipment_name,
            equipment_price: equipment_price,
        };
        let res = await this.app.mysql.get('e_equipments', { equipment_number: equipment_number });
        //判断该设备编号是否存在
        if (res == null) {
            //存在直接返回更新失败
            return {
                code: 500,
                data: res,
                message: '更新失败，此设备不存在',
            };
        } else {
            //不存在首先进行更新信息
            const result = await this.app.mysql.update('e_equipments', row, options);
            res = await this.app.mysql.get('e_equipments', { equipment_number: equipment_number });
            return {
                //更新完成之后返回状态吗、数据、提示消息
                code: 200,
                data: res,
                message: '更新设备信息成功！',
            };
        }
    }

    //根据设备编号查找设备
    async findEquipmentByNumber(postData) {
        //查询该设备编号是否存在
        const result = await this.app.mysql.get('e_equipments', { equipment_number: postData.equipment_number });
        if (result == null) {
            //不存在返回500，查询失败
            return {
                code: 500,
                data: result,
                message: '查询失败',
            }
        } else {
            //存在返回200，查询成功
            return {
                code: 200,
                data: result,
                message: '查询成功',
            }
        }
    }

    //根据设备名称查找设备
    async findEquipmentByName(postData) {
        const result = await this.app.mysql.select('e_equipments', { equipment_name: postData.equipment_name });
        console.log(result);
        if (result == null) {
            //查询失败返回500
            return {
                code: 500,
                data: result,
                message: '查询失败,此设备不存在',
            }
        } else {
            //查询成功返回200
            return {
                code: 200,
                data: result,
                message: '查询成功',
            }
        }
    }

    //新增仪器
    async insertEquipment(postData) {
        //首先判断设备编号是否存在
        const res = await this.service.equipment.findEquipmentByNumber(postData);
        //存在则提示添加失败，主键重复，并且返回500
        if (res.data != null) {
            return {
                code: 500,
                data: res.data,
                message: '添加失败，此设备已存在',
            }
        } else {//不存在再进一步判断受影响的行数
            //执行插入数据
            const result = await this.app.mysql.insert('e_equipments', postData);
            //修改的行数等于说明修改成功
            if (result.affectedRows === 1) {
                return {
                    code: 200,
                    data: postData,
                    message: '添加成功!'
                };
            } else {
                //否则失败
                return {
                    code: 500,
                    data: {},
                    message: '添加失败!',
                }
            }
        }
    }
    //根据设备编号删除仪器
    async deleteEquipmentByNumber(postData) {
        const result = await this.app.mysql.delete('e_equipments', {
            equipment_number: postData.equipment_number,
        });
        //修改的行数>0则删除成功
        if (result.affectedRows > 0) {
            //删除成功返回200，删除成功
            return {
                code: 200,
                data: {},
                message: '删除成功',
            }
        } else {
            //删除失败返回500
            return {
                code: 500,
                data: {},
                message: '删除失败,设备不存在！'
            }
        }
    }
}

module.exports = equipmentService;